package com.example.myapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class ConfigActivity extends AppCompatActivity {

    private SharedPreferences.Editor editor;
    private EditText et_name_size;
    private EditText et_phone_size;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_config);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        preferences = getSharedPreferences("config", MODE_PRIVATE);
        editor = preferences.edit();

        et_name_size = findViewById(R.id.et_name_size);
        et_phone_size = findViewById(R.id.et_phone_size);

        loadConfig();

        findViewById(R.id.btn_config).setOnClickListener(v -> {
            Toast.makeText(this, "共有" + "个电话号码", Toast.LENGTH_SHORT).show();

            try {
                float name_size = Float.parseFloat(et_name_size.getText().toString());
                editor.putFloat("name_size", name_size);
                editor.apply();

                float phone_size = Float.parseFloat(et_phone_size.getText().toString());
                editor.putFloat("phone_size", phone_size);
                editor.apply();
            } catch (Exception e) {
                ;
            } finally {
                finish();
            }
        });
    }

    private void loadConfig() {
        float name_size = preferences.getFloat("name_size", 120);
        float phone_size = preferences.getFloat("phone_size", 60);
        et_name_size.setText(String.valueOf(name_size));
        et_phone_size.setText(String.valueOf(phone_size));
    }
}